﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BestTrader.Cms.Models;
using BestTrader.Cms.IRepository;
using System.Text;
using BestTrader.Cms.Admin.Validation;
using FluentValidation.Results;
using BestTrader.Cms.ViewModels;
using BestTrader.Cms.Core.Helper;
using BestTrader.Cms.IServices;
using BestTrader.Cms.Core;
using System.Security.Claims;
using BestTrader.Cms.Admin.Filter;
using Microsoft.AspNetCore.Authorization;
using BestTrader.Cms.NPOI.Extensions;
using System.IO;

namespace BestTrader.Cms.Admin.Controllers
{
    [Authorize]
    public class RebateRecordController : BaseController
    {
        public String UserId { get { return User.Claims.FirstOrDefault(x => x.Type == "UserId")?.Value; } }
        public String RoleId { get { return User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value; } }

        private readonly IRebateRecordService _service;
        private readonly IManagerService _managerService;
        private readonly IManagerRoleService _managerRoleService;


        public RebateRecordController(
            IRebateRecordService service
          , IManagerService managerService
          , IManagerRoleService managerRoleService)
        {
            _service = service;
            this._managerService = managerService;
            this._managerRoleService = managerRoleService;
        }

        public IActionResult Index()
        {
            ViewData["IsFinanceAdmin"] = _managerRoleService.CheckIsFinanceAdmin(UserId);
            ViewData["AgentManagers"] = _managerService.GetSelfAndChildManagerByManagerIdAndRoleId(UserId, RoleId);

            return View();
        }


        public string LoadData([FromQuery]RebateRecordRequestModel model)
        {
            var data = _service.LoadData(model, UserId, RoleId);
            var resultTotal = _service.GetTotalByRecordConditions(model, UserId, RoleId);
            var result = new
            {
                code = data.code,
                data = data.data,
                count = data.count,
                totalData = resultTotal.data
            };
            return JsonHelper.ObjectToJSON(result);
        }

        [HttpGet]
        public IActionResult ExportExcel([FromQuery]RebateRecordRequestModel model)
        {
            var serverPath = _service.ExportExcel(model, UserId, RoleId);

            var stream = System.IO.File.OpenRead(serverPath);
            return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", Path.GetFileName(serverPath));
        }

        //public string GetTotalByRecordConditions([FromQuery]RebateRecordRequestModel model) {
        //    return JsonHelper.ObjectToJSON(_service.GetTotalByRecordConditions(model, UserId, RoleId));
        //}


        //[HttpGet]
        //public IActionResult AddOrModify()
        //{
        //    var roleList = _roleService.GetListByCondition(new ManagerRoleRequestModel
        //    {
        //        Key = null
        //    });
        //    return View(roleList);
        //}



        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public string AddOrModify([FromForm]ManagerAddOrModifyModel item)
        //{
        //    var result = new BaseResult();
        //    ManagerValidation validationRules = new ManagerValidation();
        //    ValidationResult results = validationRules.Validate(item);
        //    if (results.IsValid)
        //    {
        //        result = _service.AddOrModify(item);
        //    }
        //    else
        //    {
        //        result.ResultCode = ResultCodeAddMsgKeys.CommonModelStateInvalidCode;
        //        result.ResultMsg = results.ToString("||");
        //    }
        //    return JsonHelper.ObjectToJSON(result);
        //}

        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public string Delete(int[] roleId)
        //{
        //    return JsonHelper.ObjectToJSON(_service.DeleteIds(roleId));
        //}


    }
}